package com.eagle.support.common;

import android.util.Log;

import com.eagle.EApplication;
import com.eagle.R;
import com.eagle.support.helper.NotificationHelper;
import com.kinstalk.voip.sdk.EngineSdkCallMode;
import com.kinstalk.voip.sdk.EngineSdkUserRole;
import com.kinstalk.voip.sdk.api.WeaverConstants;
import com.kinstalk.voip.sdk.api.WeaverService;
import com.kinstalk.voip.sdk.logic.sip.SipLogicApi;

/**
 * Created by gc on 2017/1/5.
 */

public class MessageHelper {
    private static final String TAG = "MessageHelper";

    public static void initApplication() {
        WeaverService.getInstance().initSdkBundledSip(EApplication.getInstance(),
                WeaverConstants.WeaverServerType.ES_QINJIAN_DEV_SERVER,
                "%5%513812345678%5%5AlYEsboBAfQj90S46gYGBiao%51%5", null,
                NotificationHelper.getCallingNotification(EApplication.getInstance()), R.raw.aesthete, null);
    }

    public static void startLive() {
        Log.e(TAG, "live mode, the role is MASTER");
        String to = "live_";
        String message = "{\"live\":{\"Cmd\":1,\"ShowMode\":1,\"RoomId\":0000000000,\"Role\":4,\"Mode\":2}}";
        Log.e(TAG, "MESSAGE body: " + message);
        WeaverService.getInstance().dispatchRequest(SipLogicApi.sipSendMessage(to, message, null));
    }

    public static void startWatch(String uid, String roomid) {
        String to = "live_" + roomid;
        Log.e(TAG, "live mode, the role: ES_UR_TYPE_AUDIENCE");
        WeaverService.getInstance().dispatchRequest(SipLogicApi.sipMakeCall(to, uid, false, null, EngineSdkCallMode.ES_CM_TYPE_LIVE, EngineSdkUserRole.ES_UR_TYPE_AUDIENCE));
    }

    public static void startConf(EngineSdkUserRole role, String participator) {
        Log.e(TAG, "conference mode");
        if (participator == null) {
            Log.e(TAG, "conference participants is empty!");
            return;
        }
        if (EngineSdkUserRole.ES_UR_TYPE_MASTER == role) {
            Log.e(TAG, "conference mode, the role is MASTER");
            String to = "mconf_";
            String message = "{\"conference\":{\"Cmd\":1,\"Participator\":\"" + participator + "\",\"ShowMode\":1,\"RoomId\":0000000000,\"Role\":4,\"Mode\":3}}";
            Log.e(TAG, "MESSAGE body: " + message);
            WeaverService.getInstance().dispatchRequest(SipLogicApi.sipSendMessage(to, message, null));
        }
    }

    public static void inviteConf(EngineSdkUserRole role, String participator, String roomid) {
        Log.e(TAG, "conference mode");
        if (participator == null) {
            Log.e(TAG, "conference participants is empty!");
            return;
        }
        if (EngineSdkUserRole.ES_UR_TYPE_MASTER == role) {
            Log.e(TAG, "conference mode, the role is MASTER");
            String to = "mconf_";
            String participants = "";

            String message = "{\"conference\":{\"Cmd\":1,\"Participator\":\"" + participator + " \",\"ShowMode\":1,\"RoomId\":" + roomid + ",\"Role\":4,\"Mode\":3}}";
            Log.e(TAG, "MESSAGE body: " + message);
            WeaverService.getInstance().dispatchRequest(
                    SipLogicApi.sipSendMessage(to, message, null));
        }
    }

    public static void startLiveSecondStep(String roomid, String uid) {
        String to = "live_" + roomid;
        Log.e(TAG, "startLiveSecondStep: " + roomid + "-" + uid);
        WeaverService.getInstance().dispatchRequest(
                SipLogicApi.sipMakeCall(to, uid, false, null,
                        EngineSdkCallMode.ES_CM_TYPE_LIVE,
                        EngineSdkUserRole.ES_UR_TYPE_MASTER));
    }

    public static void sendMsgUpdate(String roomid, EngineSdkUserRole role) {
        Log.e(TAG, "conference mode, sendMsgUpdate");
        String to = "mconf_";

        String message = "{\"conference\":{\"Cmd\":1,";
        String str_role = "\"ShowMode\":1,\"RoomId\":" + roomid + ",\"Role\":" + String.valueOf(1 << role.ordinal());
        String notify = ",\"notify\": [{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" }]"
                + "}}";

        Log.e(TAG, "MESSAGE body: " + message);
        WeaverService.getInstance().dispatchRequest(
                SipLogicApi.sipSendMessage(to, message, null));
    }

    public static void sendLiveMsgBye(String roomId, String channelId, int role) {
        Log.d(TAG, "live mode, sendLiveMsgBye:" + roomId + "-" + channelId + "-" + role);
        String to = "live_";
        String message = "{\"live\":{\"Cmd\":1,"
                + "\"ShowMode\":1,\"Participator\":\"123 \",\"RoomId\":"
                + roomId + ",\"Role\":" + role + ",\"Mode\":2"
                + ",\"ByeCid\": " + channelId
                + ",\"notify\": [{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" },{ \"channelid\":\"0000000000\", \"uid\":\"0000000000\" }]"
                + "}}";

        Log.e(TAG, "MESSAGE body: " + message);
        WeaverService.getInstance().dispatchRequest(SipLogicApi.sipSendMessage(to, message, null));
    }
}
